<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">绩效系统维护</div>
        </div>
        <div class="widget-body  am-fr">
          <div class="am-u-sm-12 am-form ">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.areaTeamId" :options="areaTeams">
                  <option value="" disabled>区域</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.busTeamId" :options="busTeams">
                  <option value="">业务组</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2 v-model="query.status">
                  <option value="">在职状态</option>
                  <option value="0">否</option>
                  <option value="1">是</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="studentName" v-model="query.teacherName" placeholder="请输入姓名"/>
              </div>
            </div>


            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="search" ><span class="am-icon-search"></span>查询
                </button>
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="exportData" v-if="hasPermission('export')">导出
                </button>
              </div>
            </div>
          </div>

          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                prop="employeeName"
                label="姓名"
                min-width="100">
              </el-table-column>
              <!--<el-table-column-->
                <!--prop="idNo"-->
                <!--label="身份证"-->
                <!--min-width="200">-->
              <!--</el-table-column>-->
              <el-table-column
                prop="busTeamName"
                label="业务组"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="status"
                label="在职状态"
                min-width="120">
                <template scope="scope">
                  <div>{{scope.row.status === '0' ?  '离职' : '在职'}}</div>
                </template>
              </el-table-column>
              <el-table-column
                prop="coefficientPerformance"
                label="绩效系数"
                min-width="120">
                <template scope="scope">
                  <div>
                    <el-input v-model="scope.row.coefficientPerformance" :disabled="scope.row.edit === 0" size="small">
                    </el-input>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                prop="coefficientPerformance"
                label="工资账号"
                min-width="120">
                <template scope="scope">
                  <div>
                    <el-input v-model="scope.row.salaryAccount" :disabled="scope.row.maintain === 0" size="small">
                    </el-input>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                prop="periodName"
                label="操作"
                min-width="280">
                <template scope="scope">
                  <div>
                    <el-button type="primary" v-if="scope.row.edit === 0 && hasPermission('edit')" @click="handleEdit(scope.row)" size="small">编辑</el-button>
                    <el-button type="success" v-if="scope.row.edit === 1 && hasPermission('edit')" @click="handleConfirm(scope.row)" size="small">确认</el-button>
                    <el-button size="small" v-if="hasPermission('history')" @click="handleDetail(scope.row.employeeId)" >查看编辑记录</el-button>
                    <el-button type="primary"  v-if="scope.row.maintain === 0 && hasPermission('maintain')" @click="handleAccount(scope.row)" size="small">维护账号</el-button>
                    <el-button type="success" v-if="scope.row.maintain === 1 && hasPermission('maintain')" @click="handleConfirm(scope.row)" size="small">确认</el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <coefficient-performance-detail ref="coefficientPerformanceDetail"></coefficient-performance-detail>
          <div class="am-u-lg-12 am-cf">
            <div class="am-fr">
              <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                          @paging="loadTableData"/>
            </div>
          </div>

        </div>
      </div>
    </div>

  </div>
</template>
<script>
  import io from '../../lib/io'
  import moment from 'moment'
  import Pagination from '../base/Pagination'
  import CoefficientPerformanceDetail from './CoefficientPerformanceDetail.vue'

  export default{
    data: function () {
      return {
        tableData: [],
        total: 0,
        pageSize: 10,
        pageNo: 1,
        query: {
          areaTeamId : window.config.areaTeams[0] && window.config.areaTeams[0].areaTeamId || '' ,
          busTeamId : '',
          teacherName: '',
          status: '',
          type: 1,
        },
        periods:[],
      }
    },
    components: {
      Pagination, CoefficientPerformanceDetail
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
      this.loadTableData()
    },
    computed: {
      areaTeams: function () {
        var options = ( this.$root.config.areaTeams || [] )
          .map(function (item) {
            return {value: item.areaTeamId, text: item.name}
          })
        return options
      },
      busTeams: function () {
        var options = ( ( this.query.areaTeamId  ) ? ( this.$root.config.groupBusTeams[this.query.areaTeamId] || [] ) : [] )
          .map(function (item) {
            return {value: item.busTeamId, text: item.name}
          })
        this.query.busTeamId = ''
        return options
      },
    },
    watch:{
      'query.areaTeamId':function(){
        this.query.busTeamId =  ''
      }
    },
    methods: {
      handleDetail (employeeId) {
        this.$refs.coefficientPerformanceDetail.employeeId = employeeId
        this.$refs.coefficientPerformanceDetail.show()
      },
      handleEdit (row) {
        row.edit = 1
      },
      handleAccount(row){
        row.maintain = 1
      },
      handleConfirm (row) {
        io.post(io.updateEmployeeCoefficientPerformance, {
          employeeId: row.employeeId,
          coefficientPerformance: row.coefficientPerformance,
          type: row.type,
          salaryAccount:row.salaryAccount
        }, (ret) => {
          if (ret.success) {
            row.edit = 0
            this.$toast('更新成功')
            this.loadTableData()
          } else {
            this.$alert(ret.desc)
          }
        })
      },
      search: function () {
        this.loadTableData(1)
      },
      loadTableData: function (pageNo) {
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        _this.$showLoading()
        io.post(io.findTeacherCoefficientPerformancePage, $.extend({
          pageNo: _this.pageNo,
          pageSize: _this.pageSize
        }, _this.query), function (ret) {
          _this.$hiddenLoading()
          if (ret.success) {
            _this.total = ret.data.total
            ret.data.list.map(val => {
              val.edit = 0
              val.maintain = 0
            })
            _this.tableData = ret.data.list
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      exportData:function(){
        io.downloadFile(io.exportTeacherCoefficientPerformance , this.query )
      },
    }
  }
</script>
<style scoped>
  .red {
    color: #ea3a3a;
  }
</style>
